package org.ry.camera.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.ry.camera.base.Base64;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
/**
 * 
 * @author renyang
 *
 */
public class SharedSave {


	private static final String KEY = "camera";

	public static void save(Context mContext,String content,String key){

		SharedPreferences share = mContext.getSharedPreferences(KEY, 0);

		SharedPreferences.Editor editor = share.edit();

		editor.putString(key, content);

		editor.commit();
	}

	public static String get(Context mContext,String key){

		SharedPreferences share = mContext.getSharedPreferences(KEY, 0);

		return share.getString(key,null);

	}

	public static void saveBitmap(Context mContext,Bitmap mBitmap,String key){

		SharedPreferences share = mContext.getSharedPreferences(KEY, 0);
		SharedPreferences.Editor editor = share.edit();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		mBitmap.compress(CompressFormat.JPEG, 50, baos);
		String imageBase64 = new String(Base64.encodeBase64(baos.toByteArray()));
		editor.putString(key, imageBase64);
		editor.commit();  
	}

	public static Bitmap getBitmap(Context mContext,String key){

		SharedPreferences share = mContext.getSharedPreferences(KEY, 0);
		String imageBase64 = share.getString(key, "");
		byte[] base64Bytes = Base64.decodeBase64(imageBase64.getBytes());  
        ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
        return ((BitmapDrawable)Drawable.createFromStream(bais, KEY)).getBitmap();
	}
}
